<template>
    <view>
        <!-- pages/register/register.wxml -->
        <view class="weui-toptips weui-toptips_warn" v-if="showTopTips">{{ errorInfo }}</view>
        <form @submit.stop.prevent="formSubmit">
            <view class="textArea">
                <view class="logo">论坛管家</view>
                <view class="bottomLine">
                    <input class="weui-input" name="username" :value="username" placeholder="账号" />
                </view>
                <view>
                    <view class="bottomLine">
                        <input class="weui-input" type="password" name="password" placeholder="密码" />
                    </view>

                    <view class="bottomLine">
                        <input class="weui-input" type="password" name="password2" placeholder="重复密码" />
                    </view>

                    <view class="bottomLine">
                        <input class="weui-input" name="email" placeholder="邮箱" />
                    </view>

                    <view class="bottomLine viewflex" v-if="imageSrc">
                        <input class="weui-input" name="seccodeverify" placeholder="验证码" />
                        <image class="secodeImg" :src="imageSrc" mode="aspectFit" @tap="downSeccode"></image>
                    </view>
                </view>
                <view class="btnArea">
                    <button type="primary" formType="submit">注 册</button>
                </view>
            </view>
        </form>
    </view>
</template>

<script>
// pages/register/register.js
const registerUrl = require('../../config').registerUrl;

const loginmanager = require('../../utils/loginManager');

const datacheck = require('../../utils/datacheck.js');

const app = getApp();

export default {
    data() {
        return {
            usernamekey: '',
            passwordkey: '',
            password2key: '',
            emailkey: '',
            username: '',
            sechash: '',
            imageSrc: '',
            errorInfo: '',
            showTopTips: false
        };
    },
    onLoad: function (options) {
         // this.downSeccode()

        if (app.globalData.userInfo && app.globalData.userInfo.nickName) {
            this.username= app.globalData.userInfo.nickName;
        }

        var regnameurl = registerUrl + '&mod=' + app.globalData.regname;
        app.globalData.apimanager
            .getRequest(regnameurl)
            .then((res) => {
                if (res.Variables.reginput.username) {
                    this.setData({
                        usernamekey: res.Variables.reginput.username,
                        passwordkey: res.Variables.reginput.password,
                        password2key: res.Variables.reginput.password2,
                        emailkey: res.Variables.reginput.email
                    });
                }
            })
            .catch((res) => {
                uni.showToast({
                    title: '出错了',
                    icon: 'none'
                });
            });
    },
    methods: {
        downSeccode() {
            app.globalData.apimanager.requstSeccode('register').then((res) => {
                if (res.sechash) {
                    this.sechash= res.sechash;this.imageSrc= res.imageSrc;
                }
            });
        },

        formSubmit(e) {
            let param = e.detail.value;

            if (datacheck.isEmojiCharacter(e.detail.value.username)) {
                this.errorInfo= '用户名不能包含表情';this.showTopTips= true;
                setTimeout(function () {
                    this.showTopTips= false;
                }, app.globalData.duration);
                return;
            }

            var data = {
                regsubmit: true,
                formhash: app.globalData.formhash
            };
            data[this.usernamekey] = e.detail.value.username;
            data[this.passwordkey] = e.detail.value.password;
            data[this.password2key] = e.detail.value.password2;
            data[this.emailkey] = e.detail.value.email;

            if (loginmanager.openid) {
                data['openid'] = loginmanager.openid;
            }

            if (loginmanager.unionid) {
                data['unionid'] = loginmanager.unionid;
            }

            var regnameurl = registerUrl + '&mod=' + app.globalData.regname;
            uni.showLoading();
            app.globalData.apimanager
                .postRequest(regnameurl, data)
                .then((res) => {
                    uni.hideLoading();

                    if (res.Message.messageval.indexOf('succeed') != -1) {
                        loginmanager.loginSetUserInfo(res);
                        uni.navigateBack({
                            delta: 2
                        });
                    }

                    uni.showToast({
                        title: res.Message.messagestr,
                        icon: 'none'
                    });
                })
                .catch((res) => {
                    uni.hideLoading();
                    uni.showToast({
                        title: '出错了',
                        icon: 'none'
                    });
                });
        }
    }
};
</script>
<style>
@import './register.css';
</style>
